﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
namespace WebApplication1
{
    public partial class AddTeamMember : System.Web.UI.Page
    {
        StudentAL studentAL = new StudentAL();
        SportsAL sportsAL = new SportsAL();
        AdminAL adminAL = new AdminAL();
        List<TeamDTO> teamDTO;
        List<StudentDTO> studentDTO;
        List<StaffDTO> staffDTO;
        public Guid SelectedStudent
        {
            get
            {
                if (Session["StudentId"] == null)
                {
                    return studentAL.GetStudents()[0].StudentId;
                }
                else
                {
                    return new Guid(Session["StudentId"].ToString());
                }
            }
            set
            {
                Session["StudentId"] = value;
            }
        }

        public Guid SelectedStaff
        {
            get
            {
                if (Session["StaffId"] == null)
                {
                    return adminAL.GetAllStaff()[0].StaffId;
                }
                else
                {
                    return new Guid(Session["StaffId"].ToString());
                }
            }
            set
            {
                Session["StaffId"] = value;
            }
        }

        public string SelectedTeamMemberRole
        {
            get
            {
                if (Session["TeamMemberRole"] == null)
                {
                    return ddlTeamMemberRole.SelectedValue;
                }
                else
                {
                    return Session["TeamMemberRole"].ToString();
                }
            }
            set
            {
                Session["TeamMemberRole"] = value;
            }
        }

        public Guid SelectedTeam
        {
            get
            {
                if (Session["TeamId"] == null)
                {
                    return sportsAL.GetAllTeams()[0].TeamId;
                }
                else
                {
                    return new Guid(Session["TeamId"].ToString());
                }
            }
            set
            {
                Session["TeamId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

             teamDTO = sportsAL.GetAllTeams();
             studentDTO = studentAL.GetStudents();
             staffDTO = adminAL.GetAllStaff();

            ddlTeamMemberTeam.DataSource = teamDTO;
            ddlTeamMemberTeam.DataTextField = "TeamName";
            ddlTeamMemberTeam.DataValueField = "TeamId";
            ddlTeamMemberTeam.SelectedValue = SelectedTeam.ToString();
            ddlTeamMemberTeam.DataBind();

            ddlTeamMemberName.DataSource = studentDTO;
            ddlTeamMemberName.DataTextField = "FullName";
            ddlTeamMemberName.DataValueField = "StudentId";
            ddlTeamMemberName.SelectedValue = SelectedStudent.ToString();
            ddlTeamMemberName.DataBind();
                       
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnTeamMembersMemberAdd_Click(object sender, EventArgs e)
        {
            TeamMemberDTO teamMember = new TeamMemberDTO
            {
                TeamId = new Guid(ddlTeamMemberTeam.SelectedValue),
                TeamMemberId =new Guid( ddlTeamMemberName.SelectedValue),
                Role = ddlTeamMemberRole.SelectedValue,
                Description = txtTeamMembrDesc.Text,
                //ModifiedBy = "Tharsan",
                ModifiedTime = System.DateTime.Now
            };
            SportsAL sportsAL = new SportsAL();
            sportsAL.saveTeamMember(teamMember);
        }

        protected void btnTeamMembersAddAnother_Click(object sender, EventArgs e)
        {
            TeamMemberDTO teamMember = new TeamMemberDTO
            {
                TeamId = new Guid(ddlTeamMemberTeam.SelectedValue),
                TeamMemberId = new Guid(ddlTeamMemberName.SelectedValue),
                Role = ddlTeamMemberRole.SelectedValue,
                Description = txtTeamMembrDesc.Text,
                // ModifiedBy = "Tharsan",
                ModifiedTime = System.DateTime.Now
            };
            SportsAL sportsAL = new SportsAL();
            sportsAL.saveTeamMember(teamMember);
            ddlTeamMemberTeam.SelectedIndex = -1;
            ddlTeamMemberName.SelectedIndex = -1;
            txtTeamMembrDesc.Text = "";
        }

        protected void ddlTeamMemberRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedTeamMemberRole = ddlTeamMemberRole.SelectedValue.ToString();
            studentDTO = studentAL.GetStudents();
            staffDTO = adminAL.GetAllStaff();
            //string selectedTeamMemberRole = ddlTeamMemberRole.SelectedValue;
            if ((SelectedTeamMemberRole == "Captain") || (SelectedTeamMemberRole == "Vice Captain") || (SelectedTeamMemberRole == "Team Member") || (SelectedTeamMemberRole == "Other Student's Role"))
            {
                ddlTeamMemberName.DataSource = studentDTO;
                ddlTeamMemberName.DataTextField = "FullName";
                ddlTeamMemberName.DataValueField = "StudentId";
                ddlTeamMemberName.SelectedValue = SelectedStudent.ToString();
                ddlTeamMemberName.DataBind();
            }
            else if ((SelectedTeamMemberRole == "Coach") || (SelectedTeamMemberRole == "Prefect Of Games(POG)") || (SelectedTeamMemberRole == "Other Staff's role"))
            {
                ddlTeamMemberName.DataSource = staffDTO;
                ddlTeamMemberName.DataTextField = "FullName";
                ddlTeamMemberName.DataValueField = "StaffId";
                ddlTeamMemberName.SelectedValue = SelectedStaff.ToString();
                ddlTeamMemberName.DataBind();
            }
        }
    }
}
